package domain.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
  private static SessionFactory sessionFactory;

  static {
    try {      
      Configuration h2Config = new Configuration().configure();

      ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
          applySettings(h2Config.getProperties()).
          buildServiceRegistry();        
      sessionFactory = h2Config.buildSessionFactory(serviceRegistry);      
    } catch (Throwable ex) {
      throw new ExceptionInInitializerError(ex);
    }
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }
  
  public static void shutdown() {
    getSessionFactory().close();
  }

}
